<script setup >
import { ref, computed } from 'vue';
import SearchBar from '/src/components/SearchBar.vue'
import SingleSelect from './SingleSelect.vue'
import Pagination from '/src/components/Pagination_lxw.vue'


const props = defineProps(['columns', 'data'])

const search_query = ref('')
const sort_key = ref('')
const sort_order = ref(props.columns.reduce((o, col) => (o[col['key']] = 1, o), {}))


const filter_data = computed(() => {
    let {data} = props
    let query = search_query.value
    if(query != '') {
        query = search_query.value.toLowerCase()
        data = data.filter((row) => {
            return Object.keys(row).some((key) => {
                return String(row[key]).toLowerCase().indexOf(query) > -1
            })
        })
    }

    const key = sort_key.value
    if(key != '') {
        const order = sort_order.value[key]
        data.sort((a, b) => {
            a = a[key]
            b = b[key]
            return (a === b ? 0 : a > b ? 1 : -1) * order
        })
    }

    return data


})

function sortByKey(key) {
    sort_key.value = key
    sort_order.value[key] *= -1 
}

// Filter Options
const options1 = [{text: '广东工业大学', value: 0}, {text: '中山大学', value: 1}, {text: '广东外语外贸大学', value: 2}]
const selected1 = ref({})

const options2 = [{text: '软件工程', value: 0}, {text: '网络安全', value: 1}, {text: '计算机科学与技术', value: 2}]
const selected2 = ref({})

const options3 = [{text: '网工1班', value: 0}, {text: '网工2班', value: 1}, {text: '网工3班', value: 2}]
const selected3 = ref({})


</script>
<template>
    <div class="filter-gird">
        <div class="header" >
            <SearchBar :placeholder="'请输入'"  v-model:query="search_query" class="search-bar" />
            <SingleSelect :options="options1" :placeholder="'请选择学校'" v-model:selected="selected1" class="single-select" />
            <SingleSelect :options="options2" :placeholder="'请选择专业'" v-model:selected="selected2" class="single-select" />
            <SingleSelect :options="options3" :placeholder="'请选择班级'" v-model:selected="selected3" class="single-select" />
        </div>
        <div class="function" >
            <div class="button" style="margin-right: 14px;" >导入</div>
            <div class="button" style="margin-right: 14px;" >导出</div>
        </div>
        <div class="main" >
            <table v-if="filter_data.length" class="grid">
                <tr class="header" >
                    <th v-for="(col, index) in props.columns" :key="index" @click="sortByKey(col['key'])" >{{ col['label'] }}</th>
                </tr>
                <tr v-for="(row, index) in filter_data" :key="index" >
                    <td v-for="(col, index) in props.columns" :key="index" >{{ row[col['key']] }}</td>
                </tr>
            </table>
        </div>
        <div class="footer" >
            <Pagination :total="50" />
        </div>
        <div class="add-button">
            <div class="text" >添加用户</div>
        </div>
    </div>
</template>

<style scoped >

.filter-gird {
    position: relative;
}

.filter-gird > .header {
    width: 1057px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 20px 0;
}

.filter-gird > .header > .search-bar {
    width: 350px;
}

.filter-gird > .header > .single-select {
    width: 200px;
    background-color: #ffffff;
	box-shadow: 0px 3px 6px 0px 
		rgba(0, 0, 0, 0.16);
    border-color: transparent;
}

.filter-gird > .function {
    display: flex;
    align-items: center;
    margin: 20px 0;
}

.filter-gird > .function > .button {
    width: 100px;
	height: 32px;
	background-color: #8493a6;
	box-shadow: 0px 3px 6px 0px 
		rgba(0, 0, 0, 0.16);
	border-radius: 6px;
    opacity: 1;
    cursor: pointer;
    user-select: none;
    transition: transform 0.1s;
    font-family: Source Han Sans CN;
	font-size: 18px;
	line-height: 20px;
	color: #ffffff;
    text-align: center;
    padding: 6px 0;
}

.filter-gird > .function > .button:hover {
    opacity: 0.85;
}

.filter-gird > .function > .button:active {
    transform: translateY(10px);
}

.filter-gird > .main {
    margin: 20px 0;
}

.filter-gird > .main > .grid {
    width: 100%;
    border-collapse: collapse;
    border: 1px solid #ddd;
    font-size: 18px;
    background-color: #ffffff;
}

.main > .grid > tr {
    border-bottom: 1px solid #8a8a8a;
}

.grid > tr > td, .grid > tr > th {
    text-align: left;
    padding: 12px;
}

.main > .grid > tr.header, .main > .grid > tr:hover {
    background-color: #f1f1f1;
}

.filter-gird > .footer {
    display: flex;
    justify-content: center;
    align-items: center;
}

.filter-gird > .add-button {
    width: 57px;
    height: 57px;
    background-color: #8493a6;
	box-shadow: 0px 3px 6px 0px 
		rgba(0, 0, 0, 0.16);
    border-radius: 50%;
    cursor: pointer;
    user-select: none;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: transform 0.1s;
    position: absolute;
    bottom: 35px;
    right: 30px;
}

.filter-gird > .add-button:hover {
    opacity: 0.85;
}

.filter-gird > .add-button:active {
    transform: translateY(10px);
}

.filter-gird > .add-button > .text {
    width: 32px;
    font-family: Source Han Sans CN;
	font-size: 16px;
	color: #ffffff;
}



</style>